<?php

namespace App\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;
use Ramsey\Uuid\Uuid;

class Order extends Model
{
    protected $fillable = [
        'address', 'closed', 'reviewed', 'paid_at', 'payment_no', 'no', 'payment_method',
        'refund_status', 'refund_no', 'total_price', 'paid_price', 'extra', 'ship_status', 'ship_data'
    ];
    protected $casts = [
        'address' => 'json',
        'extra' => 'json',
        'ship_data' => 'json'
    ];

    const PAYMENT_ALIPAY = 'alipay';
    const PAYMENT_WECHAT = 'wechat';

    public static $PaymentMap = [
        self::PAYMENT_WECHAT => '微信',
        self::PAYMENT_ALIPAY => '支付宝'
    ];
    const REFUND_STATUS_PENDING = 'pending';
    const REFUND_STATUS_APPLIED = 'applied';
    const REFUND_STATUS_PROCESSING = 'processing';
    const REFUND_STATUS_FAILED = 'failed';
    const REFUND_STATUS_SUCCESS = 'success';

    const SHIP_STATUS_PENDING = 'pending';
    const SHIP_STATUS_DELIVERED = 'delivered';
    const SHIP_STATUS_RECEIVED = 'received';

    public static $shipStatusMap = [
        self::SHIP_STATUS_DELIVERED => '已发货',
        self::SHIP_STATUS_PENDING => '未发货',
        self::SHIP_STATUS_RECEIVED => '已收货',
    ];

    public static $refundStatusMap = [
        self::REFUND_STATUS_PENDING => '未退款',
        self::REFUND_STATUS_PROCESSING => '退款处理中',
        self::REFUND_STATUS_APPLIED => '已发起退款请求',
        self::REFUND_STATUS_FAILED => '退款失败',
        self::REFUND_STATUS_SUCCESS => '退款成功'
    ];

    protected $dates = [
        'paid_at'
    ];

    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::creating(function ($model) {
            if (!$model->no) {
                $model->no = self::createAvailableOrderNo();
                if (!$model->no) {
                    return false;
                }
            }
        });
    }

    private static function createAvailableOrderNo()
    {
        $prefix = date('YmdHis');

        for ($i = 0; $i < 30; $i++) {
            $no = $prefix . random_int(111, 999);
            if (!self::query()->where('no', $no)->exists()) {
                return $no;
            }
        }
        return false;
    }

    public function items()
    {
        return $this->hasMany(OrderItem::class, 'order_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
    #get Order Status
    public function getOrderStatusAttribute()
    {

        if ($this->paid_at) {
            if ($this->refund_status === Order::REFUND_STATUS_PENDING) {
                if (isset($this->extra['refund_disagree_reason'])) {
                    return '商家拒绝退款';
                }
            }
            if ($this->refund_status !== Order::REFUND_STATUS_PENDING) {
                return self::$refundStatusMap[$this->refund_status];
            }
            if ($this->reviewed) {
                return '已评价';
            }
            if ($this->ship_status === Order::SHIP_STATUS_PENDING) {
                return '待发货';
            } else {
                return Order::$shipStatusMap[$this->ship_status];
            }

            return '已支付';
        } elseif ($this->closed) {
            return '订单已关闭';
        } else {
            return '未付款';
        }
    }


    public static function getAvailableRefundNo()
    {
        do {
            $no = Uuid::uuid4()->getHex();
        } while (self::query()->where('refund_no', $no)->exists());
        return $no;
    }

    public function getTtlAttribute()
    {
        $start_time = strtotime($this->created_at);
        $end_time = strtotime($this->created_at->addSecond(config('shop.order_ttl')));
        $total_time = $end_time - $start_time;
        $el = time() - $start_time;

        return round(($el / $total_time) * 100);
    }
}
